//
//  HZOwnViewController.m
//  Stock_Pro
//
//  Created by imuse on 10-12-23.
//  Copyright 2010 gaotime. All rights reserved.
//

#import "HZOwnManagerViewController.h"
#import "HZOwnManagerView.h"
#import "HZNavHeaderView.h"
#import "QuoteLvOper.h"
#import "DictionaryTools.h"
#import "AppInfo.h"
#import "SQLTools.h"
#import "config.h"
#import "HttpTools.h"

extern QUOTELV_OPER *g_pQuoteOper;
extern sqlite3*	g_pDatabase;

void delResedData(HTTP_TOOLS *pHttp, int nType);

@interface HZOwnManagerViewController ()
@property (nonatomic, strong)HZNavHeaderView *pNavHeaderView;
@end

@implementation HZOwnManagerViewController

@synthesize pOwnManagerView;

- (void)initNavView{
    _pNavHeaderView = [[HZNavHeaderView alloc] initWithFrame:CGRectMake(0, 20, 150, 40)];
    self.navigationItem.titleView = _pNavHeaderView;
    [_pNavHeaderView updateTitleName:@"自选股管理"];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
  [super viewDidLoad];
  [self initNavView];
  float top = 30;
  if (IOS7_OR_LATER) {
    top = 64;
  }
	if (pOwnManagerView == nil) {
		pOwnManagerView = [[HZOwnManagerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-top)];
		pOwnManagerView.parentController = self;
	}
	
	[self.view addSubview:pOwnManagerView];
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  
	//更新自选股数据
	[pOwnManagerView setUiType:nUiType];
	
//	if (nUiType == OWN_UI_STOCK) {
		char *pData = QuoteLvOper_GetAllOwnCode(g_pQuoteOper);
		
		if (pData) {
			int nSize = _b2i(pData, 0);
			int nCount = nSize/7;
			NSMutableArray *pCodeArray = [[NSMutableArray alloc] initWithCapacity:50];
			
			
			char *pStr;
			pStr = pData+4;
			
			char sql[256];
			char sCode[16];
			for (int i=0; i<nCount; i++) {
				memset(sCode, 0, sizeof(sCode));
				strncpy(sCode, pStr, 6);
				
				sprintf(sql,"select * from qdic where code='%s' and marketid=%d",sCode,pStr[6]);
				/*查询*/
				sqlite3_stmt *statement;
				if(sqlite3_prepare_v2(g_pDatabase, sql, -1, &statement, NULL) == SQLITE_OK)
				{
					char sBuf[128];
					if(sqlite3_step(statement) == SQLITE_ROW)
					{
//						char *market = (char*)sqlite3_column_text(statement, FIELD_INDEX_MARKETID);
						//sprintf(sBuf, "%s,%c,%s", sCode, sMarket[0]+0x30, sName);
						
            //if (market == '24') {
                        
                        // *2014.12.1 fmy
                        char *str = (char *)sqlite3_column_text(statement, FIELD_INDEX_NAME);
                        if (pStr[6] == 6) {
                            sprintf(str, "%s(港)",str);
                        }else if (pStr[6] == 24)
                        {
                            sprintf(str,"%s(沪)",str);
                        }
                        // *end
                        
              sprintf(sBuf, "%s,%d,%s", (char*)sqlite3_column_text(statement, FIELD_INDEX_CODE), pStr[6], str);
//            }else{
//              sprintf(sBuf, "%s,%d,%s", (char*)sqlite3_column_text(statement, FIELD_INDEX_CODE), (market[0]-0x30), (char*)sqlite3_column_text(statement, FIELD_INDEX_NAME));
//            }
						//添加到列表
						[pCodeArray addObject:[NSString stringWithUTF8String:sBuf]];
					}
					
					sqlite3_finalize (statement);
				}
				
				pStr+=7;
			}
			
			Tools_Free(&pData);
			
			[pOwnManagerView setData:pCodeArray];
		}
}

-(UIStatusBarStyle)preferredStatusBarStyle{
  return UIStatusBarStyleLightContent;
}

void delResedData(HTTP_TOOLS *pHttp, int nType)
{
    if (nType==HTTP_STATE_FINISH) {
    }
}

-(void)delStockAlert
{
    if ([pOwnManagerView.delData count] > 0) {
        int nLen = [pOwnManagerView.delData count]*256;
        char *pUrl = GetUrl(URL_STOCKALERT,0);
        char *pData = Tools_Malloc(nLen);
        char sBuf[256];
        sprintf(pData,"{\"userid\":\"%s\",\"method\":\"upload\",", GetConfig(CONFIG_QS_USER_ID));
        NSMutableArray *pArray = pOwnManagerView.delData;
        strcat(pData, "\"stocks\":[");
        for (int i=0; i<[pArray count]; i++) {
            if (i > 0) {
                strcat(pData, ",");
            }
            const char *code = [[pArray objectAtIndex:i++] UTF8String];
            const char *market = [[pArray objectAtIndex:i] UTF8String];
            sprintf(sBuf, "{\"stockcode\":\"%s\",\"marketid\":\"%s\",\"method\":\"del\"}",code,market);
            strcat(pData, sBuf);
        }
        strcat(pData, "]}");
//        HttpTools_GetData(0,pUrl,pData,strlen(pData),0,delResedData,self);
        Tools_Free(&pUrl);
        Tools_Free(&pData);

    }
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)setUiType:(int)aUiType
{
	nUiType = aUiType;
}

- (void)leftNarBtnOnClick{
  [self.navigationController popViewControllerAnimated:YES];
}

@end
